package apiserver

import (
	"gitee.com/youkelike/ziam/zauth"
	"gitee.com/youkelike/ziam/zcode/zcode"
	"gitee.com/youkelike/ziam/zerrors"
	"gitee.com/youkelike/ziam/zutils/response"
	"github.com/gin-gonic/gin"

	// 启用自定义验证注册
	_ "gitee.com/youkelike/ziam/zvalidation"
)

func initRouter(g *gin.Engine) {
	installMiddleware(g)
	installAPI(g)
}

func installMiddleware(g *gin.Engine) {}

func installAPI(g *gin.Engine) {
	// 登录、登出、刷新用 jwt 认证策略
	// JWTStrategy 从配置文件中获取 jwt 的 key
	jwtStratege := zauth.NewJWTStrategy()
	g.POST("/login", jwtStratege.LoginHandler)
	g.POST("/logout", jwtStratege.LogoutHandler)
	g.POST("/refresh", jwtStratege.RefreshHandler)

	// 其它路由访问使用自动认证策略
	auto := zauth.NewAutoAuth()
	g.NoRoute(auto.AuthFunc(), func(c *gin.Context) {
		response.Write(c, zerrors.WithCode(zcode.ErrPageNotFound, "page not found"), nil)
	})

	// // 获取数据库 store 实例，数据库在前面的流程中已经创建了单例
	// storeIns := store.Client()
	// // 构造 service 实例，给 controller 用
	// srv := srvv1.NewService(storeIns)

}
